package cn.com.shopec.erp.stock.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.stock.dao.IStockOutProductDao;
import cn.com.shopec.erp.stock.dao.IStockOutPurInfoDao;
import cn.com.shopec.erp.stock.entity.StockOutPurInfo;
import cn.com.shopec.erp.stock.service.IStockOutPurInfoService;
import cn.com.shopec.erp.stock.vo.StockOutPurInfoVo;

/**
 * @ClassName: StockOutPurInfoServiceImpl.java
 * @Description: 出库商品采购单关系service接口实现类
 * @author: zengxuezhou
 * @date: 2017年1月17日 下午5:38:57
 */
@Service
public class StockOutPurInfoServiceImpl implements IStockOutPurInfoService {

	@Resource
	private IStockOutPurInfoDao stockOutPurInfoDao;

	@Resource
	private IStockOutProductDao stockOutProductDao;

	@Override
	public PageFinder<StockOutPurInfoVo> queryStockOutPurInfoList(StockOutPurInfoVo stockOutPurInfo,
			PageQuery pageQuery) {
		int count = stockOutPurInfoDao.getCountByStockNo(stockOutPurInfo);
		PageFinder<StockOutPurInfoVo> result = new PageFinder<StockOutPurInfoVo>(pageQuery.getPage(),
				pageQuery.getPageSize(), count);
		if (count > 0) {
			List<StockOutPurInfoVo> stockOutPurInfos = stockOutPurInfoDao.getListByStockNo(stockOutPurInfo,
					result.getStartOfPage(), result.getPageSize());
			result.setData(stockOutPurInfos);
		}
		return result;
	}

	@Override
	@Transactional
	public void addStockOutPurInfo(List<StockOutPurInfo> stockOutPurInfos) throws Exception {
		for (StockOutPurInfo stockOutPurInfo : stockOutPurInfos) {
			stockOutPurInfoDao.save(stockOutPurInfo);
		}
		stockOutProductDao.setProductCodeIsImport(stockOutPurInfos.get(0).getStockOutProductId(),
				SZSCMConstant.PRODUCT_PUR_STATE_2);
	}

	@Override
	@Transactional
	public void deleteStockOutPurInfo(StockOutPurInfo stockOutPurInfo) throws Exception {
		stockOutPurInfoDao.deleteStockOutPurInfo(stockOutPurInfo);
		stockOutProductDao.setProductCodeIsImport(stockOutPurInfo.getStockOutProductId(),
				SZSCMConstant.PRODUCT_PUR_STATE_0);
	}
}
